<?php

namespace app\lib\exception;

use Exception;
use think\exception\Handle;
use think\facade\Log;
use think\facade\Request;

class ExceptionHandler extends Handle
{
    private $code;
    private $msg;
    private $errorCode;
    // 请求路劲
    public function render(Exception $e) {
        if ($e instanceof  BaseException) {
            // 自定义异常
            $this->code = $e->code;
            $this->msg = $e->msg;
            $this->errorCode = $e->errorCode;
        } else {
          $this->code = 500;
          $this->msg = '内部错误';
          $this->errorCode = 999;
          Log::write($e->getMessage(), 'error');
        }
        $request = Request::instance();
        $result = [
          'msg' => $this->msg,
          'error_code' => $this->errorCode,
          'request_url' => Request::url()
        ];
        return json($result, $this->code);
    }
}